<%@ tag language="java" pageEncoding="UTF-8"%>

<%@ attribute name="name" rtexprvalue="true" required="true"%>
<%@ attribute name="size" rtexprvalue="true"%>
<%@ attribute name="label" rtexprvalue="true"%>
<%@ attribute name="required" rtexprvalue="true"%>
<%@ attribute name="value" rtexprvalue="true"%>
<%@ attribute name="single" rtexprvalue="true"%>
<%@ attribute name="help" rtexprvalue="true"%>
<%@ attribute name="widget" rtexprvalue="true"%>
<%@ attribute name="onchange" rtexprvalue="true"%>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ taglib prefix="base" uri="/WEB-INF/mytags.tld"%>

<c:set var="local"
	value="${pageContext.request.contextPath}/service/channelAction"
	scope="request" />

<script type="text/javascript">
	$.format = function(source, params) {
		if (arguments.length == 1)
			return function() {
				var args = $.makeArray(arguments);
				args.unshift(source);
				return $.format.apply(this, args);
			};
		if (arguments.length > 2 && params.constructor != Array) {
			params = $.makeArray(arguments).slice(1);
		}
		if (params.constructor != Array) {
			params = [ params ];
		}
		$.each(params, function(i, n) {
			source = source.replace(new RegExp("\\{" + i + "\\}", "g"), n);
		});
		return source;
	};
	function addAttachLine() {
		var attachIndex=parseInt($("#att_count").val());
		attachIndex=attachIndex+1;
		$('#attachTable').append($.format($("#attachTr").val(),attachIndex));
		$("#att_count").val(attachIndex);
		<c:if test="${onchange!=null}">
			$("#attachmentId"+attachIndex).change();
		</c:if>
	}
	function removeLine(n){
		<c:if test="${onchange!=null}">
			$("#attachmentId"+n).change();
		</c:if>
		$('#attachTr'+n).remove();
		var attachIndex=parseInt($("#att_count").val());
		$("#att_count").val(--attachIndex);
		
	}
	function changeAttachmentId(n){
		$("#attachmentId"+n).change();
	}
</script>
<style>
#attachTable {
	width: 27%;
}

#attachTable td {
	border-bottom: 0px;
	width: 60px;
}

a.pn-opt {
	color: #2366A8;
	text-decoration: underline;
}

.browse {
	background: url("${local}/resource/img/button.png") no-repeat scroll
		-127px -621px transparent;
	border: 0 none;
	color: #666666;
	cursor: pointer;
	height: 24px;
	line-height: 24px;
	text-indent: 18px;
	width: 59px;
	padding-left: 5px;
}

.upload-button {
	background: url("${local}/resource/img/theme-button.png") no-repeat
		scroll 0 -35px transparent;
	border: 0 none;
	color: #005BD5;
	cursor: pointer;
	height: 24px;
	text-indent: 18px;
	width: 59px;
}

.add {
	background: url("${local}/resource/img/button.png") no-repeat scroll
		-11px -111px transparent;
	border: 0 none;
	color: #666666;
	cursor: pointer;
	height: 24px;
	line-height: 24px;
	text-indent: 18px;
	width: 59px;
}
</style>

<c:if test="${size==null}">
	<c:set var="size" value="30"/>
</c:if>

<c:if test="${required==null}">
	<c:set var="required" value="false" />
</c:if>

<c:choose>
	<c:when test="${single==null}">
		<c:set var="single" value="1" />
	</c:when>
	<c:when test="${single==1}">
		<c:set var="single" value="3" />
	</c:when>
	<c:otherwise>
		<c:set var="single" value="1" />
	</c:otherwise>
</c:choose>

<th>
	<c:if test="${required}">
		<label class="requireField">*</label>
	</c:if>
 	<label>${label}:</label>
</th>

<base:bridge var="map" value="${value}"/>

<td colspan="${single}">
	<table id="attachTable" border="0">
		<tr>
			<td align="center"><input class="add" type="button"
				onclick="addAttachLine();" value="增加" /></td>
			<td align="center">附件名称</td>
			<td align="center">附件地址</td>
			<td align="center">上传</td>
		</tr>
		<c:forEach items="${base:getMapValue(map,'dding_attachment')}" var="item" varStatus="status">
			<tr id="attachTr${status.count}">
				<td align="center">
					<a onclick="removeLine('${status.count}')" href="javascript:void(0);" class="pn-opt">删除</a>
				</td>
				<td align="center">
					<input type="text" <c:if test="${onchange!=null}">onchange="changeAttachmentId('${status.count}')"</c:if> id="attachmentNames${status.count}" value="${item.attachment_name}" name="attachmentNames<c:if test='${widget!=null}'>->${widget}</c:if>"/>
				</td>
				<td align="center">
					<input type="text" <c:if test="${onchange!=null}">onchange="changeAttachmentId('${status.count}')"</c:if> id="attachmentPaths${status.count}" value="${item.attachment_path}" name="attachmentPaths<c:if test='${widget!=null}'>->${widget}</c:if>"/>
				</td>
				<td align="center">
					<span style="position:relative;display:block;width:300px;*width:300px;" id="afc${status.count}">
						<input type='text' id='attachmentText${status.count}'/>  
						<input class="browse" type='button' value='浏览'/>
						<input onchange="$('#attachmentText${status.count}').val(this.value)" size="19" type="file" name="filedata" id="filedata${status.count}" style="height:24px;opacity:0;filter:alpha(opacity=0);position:absolute;right:69px;top:2px;"/>
						<input type="button" value="上传" onclick="uploadAttachment(${status.count});" class="upload-button"/>
					</span>
					<input type="hidden" id="attachmentFilenames${status.count}" value="${item.filename}" name="attachmentFilenames<c:if test='${widget!=null}'>->${widget}</c:if>"/>
					<input type="hidden" id="attachmentId${status.count}" name="attachmentId" <c:if test="${onchange!=null}"> onchange="${onchange}"</c:if>/>
				</td>
			</tr>
		</c:forEach>
	</table>
	<input type="hidden" name="att_count" id="att_count" value="${fn:length(base:getMapValue(map,'dding_attachment'))}"/>
	<textarea id="attachTr" style="display: none">
		<tr id="attachTr{0}">
			<td align="center">
				<a onclick="removeLine({0})" href="javascript:void(0);" class="pn-opt">删除</a>
			</td>
			<td align="center">
				<input type="text" <c:if test="${onchange!=null}">onchange="changeAttachmentId('{0}')"</c:if> id="attachmentNames{0}" name="attachmentNames<c:if test='${widget!=null}'>->${widget}</c:if>" />
			</td>
			<td align="center">
				<input type="text" <c:if test="${onchange!=null}">onchange="changeAttachmentId('{0}')"</c:if> id="attachmentPaths{0}" name="attachmentPaths<c:if test='${widget!=null}'>->${widget}</c:if>" />
			</td>
			<td align="center">
				<span style="position:relative;display:block;width:300px;*width:300px;" id="afc{0}">
					<input type='text' id='attachmentText{0}'/>  
					<input class="browse" type='button' value='浏览'/>
					<input onchange="$('#attachmentText{0}').val(this.value)" size="19" type="file" name="filedata" id="filedata{0}" style="height:24px;opacity:0;filter:alpha(opacity=0);position:absolute;right:69px;top:2px;"/>
					<input type="button" value="上传" onclick="uploadAttachment({0});" class="upload-button"/>
				</span>
				<input type="hidden" id="attachmentFilenames{0}" name="attachmentFilenames<c:if test='${widget!=null}'>->${widget}</c:if>"/>
				<input type="hidden" id="attachmentId{0}" name="attachmentId" <c:if test="${onchange!=null}"> onchange="${onchange}"</c:if>/>
			</td>
		</tr>
	</textarea>
${help}</td>